package controllers

import (
	beego "github.com/beego/beego/v2/server/web"
	"sp/models"
)

type SpController struct {
	beego.Controller
}
type JsonR struct {
	Code int         `json:"code"`
	Msg  string      `json:"msg"`
	Data interface{} `json:"data"`
}

func (c *SpController) Add() {
	title := c.GetString("title")
	jj := c.GetString("jj")
	img := c.GetString("img")
	time := c.GetString("time")
	name := c.GetString("name")
	allsp := models.Sp{
		Title: title,
		Jj:    jj,
		Img:   img,
		Time:  time,
		Name:  name,
	}
	err := models.DB.Create(&allsp).Error
	if err != nil {
		c.Data["json"] = JsonR{
			Code: 0,
			Msg:  "添加失败",
			Data: nil,
		}
		c.ServeJSON()
		return
	} else {
		c.Data["json"] = JsonR{
			Code: 200,
			Msg:  "添加成功",
			Data: nil,
		}
		c.ServeJSON()
	}
}
func (c *SpController) List() {
	var allsp []models.Sp
	err := models.DB.Find(&allsp).Error
	if err != nil {
		c.Data["json"] = JsonR{
			Code: 0,
			Msg:  "查询失败",
			Data: nil,
		}
		c.ServeJSON()
		return
	} else {
		c.Data["json"] = JsonR{
			Code: 200,
			Msg:  "查询成功",
			Data: allsp,
		}
		c.ServeJSON()
	}
}
